<?php
namespace app\home\controller;

class Promotion extends ControllerBase {
    public function index() {
    	$this -> checkLogin();

    	$this -> view -> assign('layers', json_decode(getParam('USER_REWARD_LAYERS_JSON'), true));

		$promotions = [];
		$field = 'user_id, promotion_id, username, realname, phone, createtime';
		$resultUsers = \app\common\component\user\UserModel::where('promotion_id', \think\Session::get('user_id')) -> field($field) -> select();
		if($resultUsers) {
			$promotion_ids = '';
			foreach($resultUsers as $resultUser) {
				$promotions[1][] = $resultUser -> toArray();
				$promotion_ids .= $resultUser -> user_id . ',';
			}
			$promotion_ids = substr($promotion_ids, 0, -1);
	
			for($i = 2;$i <= getParam('USER_REWARD_LAYERS');$i++) {
				$promotionUsers = \app\common\component\user\UserModel::where('promotion_id', 'in', $promotion_ids) -> field($field) -> select();
	
				$promotion_ids = '';
				if($promotionUsers) {
					foreach($promotionUsers as $promotionUser) {
						$promotions[$i][] = $promotionUser -> toArray();
						$promotion_ids .= $promotionUser -> user_id . ',';
					}
					$promotion_ids = substr($promotion_ids, 0, -1);
				} else {
					break;
				}
			}
		}

    	$this -> view -> assign('promotions', $promotions);
    	return $this -> view -> fetch();
    }

    public function register() {
    	return $this -> view -> fetch();
    }
}
?>